package router

import (
	"com.cyt.common/middleware"
	"com.cyt.common/middleware/jwtauth"
	"com.cyt.common/controllers/api/v1"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func init() {
	s := g.Server()
	// 分组路由注册方式

	s.Domain("127.0.0.1,localhost").Group("/", func(group *ghttp.RouterGroup) {
		group.Middleware(
			middleware.Middleware.Ctx, // 上下文处理
			middleware.Middleware.CORS, // 跨域处理
			middleware.Middleware.ErrorHandler, // 全局异常监管
			middleware.Middleware.CSRF, // CSRF 防御
			//middleware.Middleware.Limiter, // Limiter 限流
		)

		// 设置静态文件映射
		s.AddStaticPath("/storage", "/public/storage")
		// Metrics监控
		s.BindHandler("/metrics", func(r *ghttp.Request) {
			promhttp.Handler().ServeHTTP(r.Response.ResponseWriter, r.Request)
		})

		group.POST("/upload", v1.Upload, "Upload") // 上传文件
		
		// api
		group.Group("/api/v1", func(group *ghttp.RouterGroup) {
			group.POST("/member/create", v1.Member, "MemberCreate") //新增
			group.POST("/member/login", jwtauth.JwtAuth.LoginHandler)

			group.Group("/query", func(group *ghttp.RouterGroup) {
				group.ALL("/search", v1.Query, "Search")
			})

			group.Middleware(
				middleware.Middleware.JwtAuth, // jwt鉴权
			)
			group.GET("/refresh_token", jwtauth.JwtAuth.RefreshHandler) //刷新token
			group.GET("/logout", jwtauth.JwtAuth.LogoutHandler) //退出

			group.Group("/member", func(group *ghttp.RouterGroup) {
				// 简单会员示例
				group.POST("/update", v1.Member, "MemberUpdate") //更新
				group.POST("/delete", v1.Member, "MemberDelete") //删除
				group.GET("/info/{member_id}", v1.Member, "MemberInfo") //会员信息
				group.GET("/list", v1.Member, "MemberList") //会员列表
			})
		})
	})
}
